package com.voxel.sense.infomsg.view.decoration;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.example.com.common.utils.L;
import com.voxel.sense.infomsg.R;

import cyy.example.com.facotry.Factory;

/**
 * @author ChenYiYao
 * @date 2018/5/14
 * des : recycler分割线，用户主要用于线性列表
 * <p>
 * 注意：该类传入Drawable 而没有传入分割线的宽度，则这里统一使用Drawable的width属性为宽度
 */
public class LinearItemDecoration extends RecyclerView.ItemDecoration {

    //各分割线的宽度
    private final int DEFAULT_WIDTH = 1;
    private int mTopDividerWidth = -1;
    private int mBottomDividerWidth = -1;
    private int mLeftDividerWidth = -1;
    private int mRightDividerWidth = -1;

    private Drawable mLDivider; //左边框
    private Drawable mTDivider; //上边框
    private Drawable mRDivider; //右边框
    private Drawable mBDivider; //底部边框

    //是否绘画第0个item的分割线,若是垂直滑动类型，则决定顶部的分割线。若是水平类型则决定左边边分割线
    private boolean mIsDrawFirstItem = false;
    //是否绘画最后一个个item分割线，若是垂直滑动类型，则决定底部的分割线。若是水平类型则决定右边分割线
    private boolean mIsDrawLastItem = true;

    //控制是否绘画vertical模式的左边框
    private boolean mIsDrawLineaVerticalLeftBorder = false;
    //控制是否绘画vertical模式的右边框
    private boolean mIsDrawLineaVerticalRightBorder = false;

    public LinearItemDecoration() {
        this(Factory.app().getResources().getDrawable(R.drawable.recycle_divider));

    }

    public LinearItemDecoration(int drawId) {
        this(Factory.app().getResources().getDrawable(drawId));
    }

    public LinearItemDecoration(Drawable draw) {
        if (draw == null) {
            draw = Factory.app().getResources().getDrawable(R.drawable.recycle_divider);
        }
        int intrinsicWidth = draw.getIntrinsicWidth();
        mLDivider = draw;
        mTDivider = draw;
        mRDivider = draw;
        mBDivider = draw;
        this.mTopDividerWidth = intrinsicWidth;
        this.mBottomDividerWidth = intrinsicWidth;
        this.mLeftDividerWidth = intrinsicWidth;
        this.mRightDividerWidth = intrinsicWidth;
    }

    public LinearItemDecoration(int drawId, int width) {
        this(Factory.app().getResources().getDrawable(drawId), width);
    }


    public LinearItemDecoration(Drawable draw, int width) {
        if (draw == null) {
            draw = Factory.app().getResources().getDrawable(R.drawable.recycle_divider);
        }
        mLDivider = draw;
        mTDivider = draw;
        mRDivider = draw;
        mBDivider = draw;

        this.mTopDividerWidth = width;
        this.mBottomDividerWidth = width;
        this.mLeftDividerWidth = width;
        this.mRightDividerWidth = width;
    }

    public LinearItemDecoration(int topDividerWidth, int BottomDividerWidth, int leftDividerWidth, int rightDividerWidth, int drawId) {
        this(topDividerWidth, BottomDividerWidth, leftDividerWidth, rightDividerWidth, Factory.app().getResources().getDrawable(drawId));
    }

    public LinearItemDecoration(int topDividerWidth, int dividerBottomWidth, int leftDividerWidth, int rightDividerWidth, Drawable draw) {
        if (draw == null) {
            draw = Factory.app().getResources().getDrawable(R.drawable.recycle_divider);
        }
        this.mTopDividerWidth = topDividerWidth;
        this.mBottomDividerWidth = dividerBottomWidth;
        this.mLeftDividerWidth = leftDividerWidth;
        this.mRightDividerWidth = rightDividerWidth;
        mLDivider = draw;
        mTDivider = draw;
        mRDivider = draw;
        mBDivider = draw;
    }

    public LinearItemDecoration(int LDrawId, int TDrawId, int RDrawId, int BDrawId) {
        this(Factory.app().getResources().getDrawable(LDrawId),
                Factory.app().getResources().getDrawable(TDrawId),
                Factory.app().getResources().getDrawable(RDrawId),
                Factory.app().getResources().getDrawable(BDrawId));
    }

    public LinearItemDecoration(Drawable LDivider, Drawable TDivider, Drawable RDivider, Drawable BDivider) {
        if (mLDivider == null) {
            mLDivider = Factory.app().getResources().getDrawable(R.drawable.recycle_divider);
        }
        this.mLDivider = LDivider;
        this.mLeftDividerWidth = LDivider.getIntrinsicWidth();

        if (mTDivider == null) {
            mTDivider = Factory.app().getResources().getDrawable(R.drawable.recycle_divider);
        }
        this.mTDivider = TDivider;
        this.mTopDividerWidth = TDivider.getIntrinsicWidth();

        if (mRDivider == null) {
            mRDivider = Factory.app().getResources().getDrawable(R.drawable.recycle_divider);
        }
        this.mRDivider = RDivider;
        this.mRightDividerWidth = RDivider.getIntrinsicWidth();

        if (mBDivider == null) {
            mBDivider = Factory.app().getResources().getDrawable(R.drawable.recycle_divider);
        }
        this.mBDivider = BDivider;
        this.mBottomDividerWidth = BDivider.getIntrinsicWidth();
    }

    public LinearItemDecoration(int width, Drawable LDivider, Drawable TDivider, Drawable RDivider, Drawable BDivider) {
        this.mLDivider = LDivider;
        this.mLeftDividerWidth = width;
        this.mTDivider = TDivider;
        this.mTopDividerWidth = width;
        this.mRDivider = RDivider;
        this.mRightDividerWidth = width;
        this.mBDivider = BDivider;
        this.mBottomDividerWidth = width;
    }


    /**
     * 该方法中绘制的图像，若没有再getItemOffsets设置偏移量，则绘制的图像会被item覆盖
     */
    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        drawLinearItemSpace(c, parent, layoutManager);
    }


    /**
     * 该方法绘制的图像会覆盖在原有item之上
     *
     * @param c
     * @param parent
     * @param state
     */
    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDrawOver(c, parent, state);
    }


    private void drawLinearItemSpace(Canvas c, RecyclerView parent, RecyclerView.LayoutManager layoutManager) {
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams laParams = (RecyclerView.LayoutParams) child.getLayoutParams();
            if (layoutManager.canScrollVertically()) {
                drawLinearVerticallBorder(c, parent, child);
            } else if (layoutManager.canScrollHorizontally()) {
                //水平item
                drawLinearHorizontallyBorder(c, parent, child);

            }
        }
    }


    /**
     * 画垂直模式的边框
     *
     * @param c
     * @param parent
     * @param child
     */
    private void drawLinearVerticallBorder(Canvas c, RecyclerView parent, View child) {
        //左边框
        int left = child.getLeft() - mLeftDividerWidth;
        int right = child.getLeft();
        int top = child.getTop();
        int bottom = child.getBottom();
        if (mIsDrawLineaVerticalLeftBorder) {
            mLDivider.setBounds(left, top, right, bottom);
            mLDivider.draw(c);
        }
        //右边框
        if (mIsDrawLineaVerticalLeftBorder) {
            left = child.getRight();
            right = left + mRightDividerWidth;
            mRDivider.setBounds(left, top, right, bottom);
            mRDivider.draw(c);
        }
        //上下边框
        left = child.getLeft() - mLeftDividerWidth;
        right = child.getRight() + mRightDividerWidth;
        if (mIsDrawFirstItem && isFirstItem(parent, child)) {
            //首个item上边框
            top = child.getTop() - mTopDividerWidth;
            bottom = child.getTop();
            mTDivider.setBounds(left, top, right, bottom);
            mTDivider.draw(c);
        }
        //下边框
        if (!isLastItem(parent, child) || (isLastItem(parent, child) && mIsDrawLastItem)) {
            top = child.getBottom();
            bottom = child.getBottom() + mBottomDividerWidth;
            mBDivider.setBounds(left, top, right, bottom);
            mBDivider.draw(c);
        }
    }


    /**
     * 绘制水平模式的边框
     * @param c
     * @param parent
     * @param child
     */
    private void drawLinearHorizontallyBorder(Canvas c, RecyclerView parent, View child) {
    }


    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        getLinearItemOffsets(outRect, view, parent, layoutManager);
    }

    private void getLinearItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.LayoutManager layoutManager) {
        if (layoutManager.canScrollVertically()) {
            int left = mIsDrawLineaVerticalLeftBorder ? mLeftDividerWidth : 0;
            int right = mIsDrawLineaVerticalRightBorder ? mRightDividerWidth : 0;
            //垂直滑动
            if (isFirstItem(parent, view) && mIsDrawFirstItem) {
                //第一个位置的item需要添加上边框
                outRect.set(left, mTopDividerWidth, right, mBottomDividerWidth);
            } else if (!mIsDrawLastItem && isLastItem(parent, view)) {
                outRect.set(left, 0, right, 0);
                //最后一个item不需要下边框
            } else {
                outRect.set(left, 0, right, mBottomDividerWidth);
            }
            L.d("-------------------------------------------------------------");
            L.d("left"+left+"----top"+mTopDividerWidth+"----right"+right+"----bottom"+mBottomDividerWidth);
        } else if (layoutManager.canScrollHorizontally()) {
            //水平滑动
            if (isFirstItem(parent, view) && mIsDrawFirstItem) {
                //第一个位置的item需要添加上边框
                outRect.set(mLeftDividerWidth, mTopDividerWidth, mRightDividerWidth, mBottomDividerWidth);
            } else if (!mIsDrawLastItem && isLastItem(parent, view)) {
                outRect.set(mLeftDividerWidth, mTopDividerWidth, 0, mBottomDividerWidth);
                //最后一个item不需要下边框
            } else {
                outRect.set(0, mTopDividerWidth, mRightDividerWidth, mBottomDividerWidth);
            }
        }
    }


    /**
     * 判断是否第一个item
     *
     * @param parent
     * @param item
     * @return
     */
    private boolean isFirstItem(RecyclerView parent, View item) {
        int childAdapterPosition = parent.getChildAdapterPosition(item);
        return childAdapterPosition == 0;
    }

    /**
     * 判断是否最后一个item
     *
     * @param parent
     * @param item
     * @return
     */
    private boolean isLastItem(RecyclerView parent, View item) {
        int childAdapterPosition = parent.getChildAdapterPosition(item);
        int itemCount = parent.getAdapter().getItemCount();
        return (childAdapterPosition - 1) == itemCount;
    }

    public LinearItemDecoration setDrawFirstItem(boolean drawFirstItem) {
        mIsDrawFirstItem = drawFirstItem;
        return this;
    }

    public LinearItemDecoration setDrawLastItem(boolean drawLastItem) {
        mIsDrawLastItem = drawLastItem;
        return this;
    }

    public LinearItemDecoration setDrawVerticalModelLeftAndRightBorder(boolean left,boolean right) {
        mIsDrawLineaVerticalLeftBorder =left;
        mIsDrawLineaVerticalRightBorder =right;
        return this;
    }
}
